<template>
  <section class="welfare-account-manage">
    <div class="manage-title">新建/编辑商户子帐号信息</div>

    <section class="manage-main">
      <el-form ref="form" v-loading="formloading" :model="form" :rules="rules" label-width="150px" class="main-form">
        <template v-if="state == 'create'">
          <el-form-item label="子账户名称" prop="subAcntNm">
            <el-input v-model="form.subAcntNm" placeholder="请输入子账户名称" />
          </el-form-item>
        </template>
        <template v-if="state == 'update'">
          <el-form-item label="子账户类型" prop="subAcntTp">
            <el-input v-model="form.subAcntTp" disabled placeholder="请输入子账户类型" />
          </el-form-item>
          <el-form-item label="子账户名称" prop="subAcntNm">
            <el-input v-model="form.subAcntNm" placeholder="请输入子账户名称" />
          </el-form-item>
          <el-form-item label="有效期时间段" required>
            <el-col :span="10">
              <el-form-item label prop="subAcntStartTm">
                <el-date-picker :disabled="form.subAcntTp === 'AC01'" v-model="form.subAcntStartTm" type="datetime" placeholder="选择有效期开始日期" style="width: 100%;" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" />
              </el-form-item>
            </el-col>
            <el-col class="line" :span="4">-</el-col>
            <el-col :span="10">
              <el-form-item label prop="subAcntEndTm">
                <el-date-picker :disabled="form.subAcntTp === 'AC01'" v-model="form.subAcntEndTm" type="datetime" placeholder="选择有效期截止日期" style="width: 100%;" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" />
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="子帐号状态" prop="subAcntSt">
            <el-radio-group v-model="form.subAcntSt">
              <el-radio label="0">可用</el-radio>
              <el-radio label="1">不可用</el-radio>
            </el-radio-group>
          </el-form-item>
        </template>
      </el-form>
    </section>

    <div class="manage-footer">
      <el-button style="padding: 8px 30px;" @click="handleCancel">取消</el-button>
      <el-button style="padding: 8px 30px;" type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </section>
</template>
<script>
import { insert, update, get } from './../api'
export default {
  name: 'WelfareAccountManage',
  data() {
    return {
      state: 'create',
      formloading: false,
      form: {
        subAcntNm: '',
        subAcntTp: '',
        subAcntTps: '',
        subAcntStartTm: '',
        subAcntEndTm: '',
        subAcntSt: '0'
      },
      rules: {
        subAcntNm: [
          { required: true, message: '请输入子账户名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        subAcntSt: [{ required: true, message: '请选择子账号状态', trigger: 'blur' }],
        subAcntStartTm: [{ required: true, message: '请选择有效期开始日期', trigger: 'blur' }],
        subAcntEndTm: [{ required: true, message: '请选择有效期截止日期', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.state = this.$route.query.t === 'c' ? 'create' : 'update'
    if (this.$route.query.t === 'u') {
      if (this.$route.query.i) {
        this.getData(this.$route.query.i)
      } else {
        this.$message({
          showClose: true,
          message: '参数异常！',
          type: 'warning',
          onClose: () => {
            this.$router.back()
          }
        })
      }
    }
  },
  methods: {

    handleSubmit() {
      this.state === 'create' ? this.createData() : this.updateData()
    },

    handleCancel() {
      this.$router.push({path:this.$filterPath("index")})
    },

    /*
    * 新增
    * */
    createData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          const {
            id,
            subAcntNm
          } = this.form
          return new Promise((resolve, reject) => {
            insert({
              id,
              subAcntNm
            }).then(() => {
              loading.close()
              this.$message({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 1500
              })
              this.$router.push({ path: '/welfare/accmanage/account/index' })
            }).catch(error => {
              reject(error)
              loading.close()
            })
          })
        }
      })
    },

    /**
     * 编辑提交
     */
    updateData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          const {
            subAcntNm,
            subAcntTp,
            subAcntStartTm,
            subAcntEndTm,
            subAcntSt
          } = this.form
          return new Promise((resolve, reject) => {
            update({
              subAcntNm,
              subAcntTp,
              subAcntStartTm,
              subAcntEndTm,
              subAcntSt
            }).then(() => {
              loading.close()
              this.dialogFormVisible = false
              this.$message({
                title: '成功',
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              this.$router.push({ path: '/welfare/accmanage/account/index' })
            }).catch(error => {
              reject(error)
              loading.close()
            })
          })
        }
      })
    },

    cancel() {
      this.$router.push({ path: '/welfare/accmanage/account/index' })
    },

    /**
     * 获取详情
     */
    getData(subAcntTp) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        get(subAcntTp).then(res => {
          loading.close()
          const data = res.data.data
          for (const key in this.form) {
            if (key === 'subAcntStartTm' || key === 'subAcntEndTm') {
              if (data[key]) {
                this.form[key] = data[key].replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, '$1-$2-$3 $4:$5:$6')
              }
            } else {
              this.form[key] = data[key]
            }
          }
          console.log(this.form)
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    }
  }
}
</script>
<style lang="scss">
@import '@/styles/mixin.scss';
.welfare-account-manage {
  background-color: #fff;
  min-height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  overflow: hidden;
  .manage-title {
    position: relative;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    background-color: #ffffff;
    border-bottom: 1px solid #e5e5e5;
    font-family: PingFangSC-Medium;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;
    &:before {
      content: '';
      position: absolute;
      width: 2px;
      @include bg-color($mainColor);
      top: 13px;
      bottom: 13px;
      left: 15px;
    }
  }

  .manage-main {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    bottom: 52px;
    box-sizing: border-box;
    padding: 0 10px;
    overflow: hidden;
    &::-webkit-scrollbar {
      width: 8px; /*对垂直流动条有效*/
      height: 1px; /*对水平流动条有效*/
    }
    /** 滚动条轨道*/
    &::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      background-color: white;
      border-radius: 3px;
    }
    /** 滚动条里面小方块*/
    &::-webkit-scrollbar-thumb {
      height: 32px;
      border-radius: 5px;
      box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.1);
      background-color: #eaeaea;
    }
    &:hover {
      overflow: auto;
    }
  }

  .main-form {
    width: 1000px !important;
    margin: 0 auto;
    .line {
      text-align: center;
    }
    .form-card {
      margin-top: 20px;
      .el-card__body {
        max-width: 90%;
        margin: 0 auto;
      }
    }
  }

  .manage-footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    text-align: center;
    padding: 10px 0;
    background-color: white;
    box-shadow: 0 -3px 6px #f5f5f5;
  }
}
</style>
